package com.siyuanren.gcocp.web;

import com.siyuanren.gcocp.common.web.bean.Result;
import com.siyuanren.gcocp.dto.FavoriteDTO;
import com.siyuanren.gcocp.entity.WxCourseContent;
import com.siyuanren.gcocp.entity.WxUserFavorite;
import com.siyuanren.gcocp.exception.CourseException;
import com.siyuanren.gcocp.service.FavoriteService;
import com.siyuanren.gcocp.util.VaildUtil;
import com.siyuanren.gcocp.vo.QueryFavoritesVO;
import com.siyuanren.gcocp.web.wx.BaseWxController;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * Created by   anxing
 * Project_name gcocp-parent
 * Path         com.siyuanren.gcocp.web
 * Date         2018/4/18
 * Time         13:55
 * Description 用户收藏
 */
@Slf4j
@Controller
@RequestMapping("/favorite")
public class FavoriteController extends BaseWxController {
    @Autowired
    FavoriteService service;
    /**
     * 加入收藏
     * @param userFavorite
     */
    @ResponseBody
    @RequestMapping("add")
    public Result addFavoriteContent(WxUserFavorite userFavorite) throws Exception {
        VaildUtil.check(userFavorite);
        userFavorite.setUserId(getUserId());
        int row = service.addFavoriteContent(userFavorite);
        return new Result(row);
    }

    /**
     * 移除收藏内容
     * @param favoriteId
     */
    @RequestMapping("remove")
    @ResponseBody
    public Result removeFavoriteContent(Integer favoriteId) throws Exception {
        if (favoriteId == null) {
            throw new Exception("favoriteId 参数不正确");
        }
        service.removeFavoriteContent(favoriteId);
        return new Result();
    }

    /**
     * 返回收藏内容列表
     * @param queryFavoritesVO
     * @return
     */
    @ResponseBody
    @RequestMapping("list")
    public Result getFavorierList(QueryFavoritesVO queryFavoritesVO) throws Exception {
        VaildUtil.check(queryFavoritesVO);
        queryFavoritesVO.setUserId(getUserId());
        List<WxCourseContent> favorierList = service.getFavorierList(queryFavoritesVO);
        return new Result(favorierList);
    }
}
